package com.atqinke.designpattern.creationtype.singlecase;

import org.springframework.util.SerializationUtils;

/**
 * @param
 * @description: 利用枚举类实现单例模式
 * @author: NieHK
 * @date: 2022/8/16 10:18
 * @return:
 */
public enum Enumeration {

    INSTANCE("张三", 18, "18年后还是一条好汉");

    private final String name;
    private final Integer age;
    private final String annotation;

    Enumeration(String name, Integer age, String annotation) {
        this.name = name;
        this.age = age;
        this.annotation = annotation;
    }

    public static Enumeration getEnumeration() {
        return INSTANCE;
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //获取枚举类对象
        Enumeration enumeration = Enumeration.getEnumeration();
        //获取枚举类对象
        Enumeration newEnumeration = Enumeration.getEnumeration();
        //进行测试
        System.out.println("正常情况下，实例化两个实例是否相同：" + (enumeration == newEnumeration)); //true



        //对enumeration序列化
        byte[] serialize = SerializationUtils.serialize(enumeration);
        //通过反序列化得到newStaticInnerClass对象
        Enumeration oldEnumeration = (Enumeration) SerializationUtils.deserialize(serialize);
        //比较两对象是否相等
        System.out.println("枚举序列化前后两个是否同一个："+(enumeration == oldEnumeration));//true
    }
}
